<?php
include '../../conn.php';
if(!isset($_GET['notice_id'])){
    $arr = [
        "desc" => "缺少参数"
    ];
    echo json_encode($arr);
    exit;
}
function formatTime($date) {
    $timer = strtotime($date);
    $diff = $_SERVER['REQUEST_TIME'] - $timer;
    $day = floor($diff / 86400);
    $free = $diff % 86400;
    if($day > 0) {
        return $day."天前";
    }else{
        if($free>0){
            $hour = floor($free / 3600);
            $free = $free % 3600;
            if($hour>0){
                return $hour."小时前";
            }else{
                if($free>0){
                    $min = floor($free / 60);
                    $free = $free % 60;
                    if($min>0){
                        return $min."分钟前";
                    }else{
                        if($free>0){
                            return $free."秒前";
                        }else{
                            return '刚刚';
                        }
                    }
                }else{
                    return '刚刚';
                }
            }
        }else{
            return '刚刚';
        }
    }
}
$notice_id = $_GET['notice_id'];
$sql = "select a.id,a.level,a.comment_id,a.from_user_id,b.nickname,b.avatar,a.comment,a.create_time from users_comment as a
        left join users_info as b
        on a.from_user_id = b.id
        where content_id = $notice_id and level = 1";
$req = $pdo->getAll($sql);
$arr = array();
foreach($req as $res):
    $sql = "select a.id,a.level,a.comment_id,a.from_user_id,a.to_user_id,b.nickname,b.avatar,a.comment,a.create_time from users_comment as a
    left join users_info as b
    on a.from_user_id = b.id
    where content_id = $notice_id and comment_id = $res[id]";
    $red = $pdo->getAll($sql);
    $sum = count($red);
    $children = array();
    foreach($red as $reb):
        if($reb['level'] == 3) {
            $sql = "select nickname as toname from users_info
                where id = $reb[to_user_id]";
            $toname = $pdo->getOne($sql);
        }
        array_push(
            $children,[
                "id" => $reb['id'],
                "level" => $reb['level'],
                "comment_id" => $reb['comment_id'],
                "from_user_id" => $reb['from_user_id'],
                "nickname" => $reb['nickname'],
                "toname" => $reb['level'] == 3 ? $toname['toname'] : '',
                "avatar" => $url.$reb['avatar'],
                "comment" => $reb['comment'],
                "create_time" => formatTime($reb['create_time'])
            ]
        );
    endforeach;
    array_push(
        $arr,[
            "id" => $res['id'],
            "level" => $res['level'],
            "comment_id" => $res['comment_id'],
            "from_user_id" => $res['from_user_id'],
            "nickname" => $res['nickname'],
            "avatar" => $url.$res['avatar'],
            "comment" => $res['comment'],
            "showChildren" => false,
            "sum" => $sum,
            "create_time" => formatTime($res['create_time']),
            "children" => $children
        ]
    );
endforeach;
echo json_encode($arr);